﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using IBLL;
using Castle.Windsor;
using System.Threading.Tasks;
using DAL;
using System.Collections.Concurrent;
using System.Text;
using System.Diagnostics;
using webMvc.Authorization;
using DalFactory;
using webMvc.Models.ViewModel;
using Tools;
using webMvc.Models.Jurisdiction;
using kang.Entitys.IdentityAuthentication;
using webMvc.App_Start.WebQueue;

namespace webMvc.Controllers
{
	public class testController : Controller
	{
		//static IWindsorContainer container = new WindsorContainer();
		//IdonatorsService bll = container.Resolve<IdonatorsService>();
		// GET: test
		//public ActionResult Index()
		//{
		//IdonatorsService bll = new donatorsService();
		//List<Donators> myList = bll.LoadEntities(s => true).ToList();

		//int count = 0;
		//List<Donators> myList1 = bll.LoadPageEntities(1, 10, out count, s => true, s => s.DonateDate, true).ToList();
		//ViewBag.allList = myList;
		//return View(myList1);
		//}


		//public string getAllList()
		//{
		//Object thisLock = new Object();
		//Stopwatch sw = new Stopwatch();
		//sw.Start();

		//#region 1552并发
		////
		////StringBuilder sb = new StringBuilder();
		////Task[] buff = new Task[10];
		////for (int i = 1; i < 11; i++)
		////{
		////	var Page = i;
		////	buff[i - 1] = Task.Factory.StartNew(() =>
		////	  {
		////		  MultithreadingDbReader<Donators> db = new MultithreadingDbReader<Donators>();
		////		  var result = db.LoadPageEntities(Page, 1000, s => true, s => s.DonateDate, true).ToArray();
		////		  Parallel.ForEach(result, (x) =>
		////		  {
		////			  lock (thisLock)
		////			  {
		////				  sb.Append(x.Name);
		////			  }
		////		  });
		////	  });
		////}
		////Task.WaitAll(buff);
		//#endregion

		//StringBuilder sb = new StringBuilder();
		//MultithreadingDbReader<Donators> db = new MultithreadingDbReader<Donators>();

		//for (int i = 1; i < 11; i++)
		//{
		//	var result = db.LoadPageEntities(i, 1000, s => true, s => s.DonateDate, true).ToArray();
		//	foreach (Donators item in result)
		//	{
		//		sb.Append(item.Name);
		//	}
		//}
		//sw.Stop();

		//return sb.ToString() + sw.ElapsedMilliseconds.ToString();
		//}

		//public string allTestData()
		//{
		//	IdonatorsService bll = new donatorsService();

		//	for (int i = 0; i < 50000; i++)
		//	{
		//		bll.AddDonator(new Donators() { Name = string.Format("这是第{0}条数据!", i + 1), Amount = i, DonateDate = DateTime.Now });
		//	}
		//	bll.CurrentDbSession.allSaveChangge();

		//	return "添加成功!";
		//}

		IUserInfoService userInfosService = new UserInfoService();

		[HttpPost]
		public JsonResult login(UserLoginViewModel userLoginViewModel)
		{
			var result = new JsonResult();

			string encryPwd = MD5Helper.MakeMD5(userLoginViewModel.PassWord, 32);

			var getLoginModel = userInfosService.LoadEntities(x => x.Uname == userLoginViewModel.UserName && x.Upwd == encryPwd).FirstOrDefault();

			if (getLoginModel != null)
			{
				TypeDesToken typeDesToken = new TypeDesToken();
				//生成token
				string Token = typeDesToken.FoXiMakeToken(getLoginModel.Uname, getLoginModel.Upwd);
				if (userLoginViewModel.IsCheck)
				{
					HttpCookie TokenCookie = new HttpCookie("token");
					TokenCookie.Value = Token;
					TokenCookie.Path = "/";
					TokenCookie.Expires = DateTime.Now.AddHours(2);
					TokenCookie.HttpOnly = true;
					Response.Cookies.Add(TokenCookie);
				}
				result.Data = new { msg = "登录成功!", token = Token };
				Session["Token"] = new ServiceUserToken() { uid = getLoginModel.Id, Token = Token };
			}
			else
			{
				result.Data = new { msg = "登录失败!请检查用户名和密码是否正确!" };
			}
			return result;
		}

		[MyAuthentication]
		public ActionResult Home()
		{
			return View();
		}

		[LoginAuthentication]
		public ActionResult login()
		{
			//string jiaMiStr = typeDesToken.FoXiMakeToken("test", "kang");
			//string jiemi = typeDesToken.FoXiGetToken(jiaMiStr);
			//var userInfo = userInfosService.LoadEntities(x => true).ToList();
			return View();
		}

		public string testConcurrency()
		{

			Parallel.For(0, 10000, (t, state) =>
			{
				AddProductHandle.GetAddProductHandle().AddProduct(new ProductInfo()
				{
					valueOne = string.Format("第一项第{0}个商品", t),
					valueTwo = string.Format("第二项第{0}个商品", t)
				});
			});

			return "ojbk";

		}

		public ActionResult NoAuthority()
		{
			return View();
		}

		public ActionResult UserList()
		{
			List<UserInfo> UserList = userInfosService.LoadEntities(x => true).ToList();
			return View(UserList);
		}

	}
}